package com.lm;

import com.os.OSAttribute;
import com.os.ObjectSprite;
import java.awt.Rectangle;
import javax.swing.event.ChangeEvent;
import mapgenerator.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JToggleButton;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Sajarwo Anggai
 */
public class WorkSpace implements ActionListener, MouseListener, MouseMotionListener, ChangeListener {

    private Main comp;
    private int tileX, tileY, mapX, mapY;

    public WorkSpace(Main comp) {
        this.comp = comp;
        comp.ToolbarDrawPattern.addActionListener(this);
        comp.ToolbarDrawSprite.addActionListener(this);
        comp.ToolbarRP.addActionListener(this);
        comp.ToolbarRS.addActionListener(this);
        comp.ToolbarSetBlock.addActionListener(this);
        comp.ToolbarEraseBlock.addActionListener(this);
        comp.TabbedPane.addChangeListener(this);
    }

    public static int pixelsToTiles(int pixels, int tileSize) {
        return (int) Math.floor((float) pixels / tileSize);
    }

    public static int tilesToPixels(int numTiles, int tileSize) {
        return numTiles * tileSize;
    }
    private JToggleButton currentToolbarButton;

    public void actionPerformed(ActionEvent e) {
        Object ob = e.getSource();
        currentToolbarButton = (JToggleButton) ob;
        if (currentToolbarButton.isSelected()) {
            comp.ToolbarDrawPattern.setSelected(false);
            comp.ToolbarDrawSprite.setSelected(false);
            comp.ToolbarRP.setSelected(false);
            comp.ToolbarRS.setSelected(false);
            comp.ToolbarSetBlock.setSelected(false);
            comp.ToolbarEraseBlock.setSelected(false);
            currentToolbarButton.setSelected(true);
        }
    }

    public void drawTile(MouseEvent e) {
        try {
            if (comp.ToolbarDrawPattern.isSelected()) {
                patternOperation(3);
            } else if (comp.ToolbarRP.isSelected()) {
                patternOperation(4);
            } else if (comp.ToolbarDrawSprite.isSelected()) {
                comp.osControlPanel.setSpritePosition(e.getX(), e.getY(), true);
            } else if (comp.ToolbarRS.isSelected()) {
                LayerContainer lc = comp.layerContainer;
                ArrayList<ObjectSprite> al = comp.osContainer.getSprites();
                for (int i = 0; i < al.size(); i++) {
                    ObjectSprite os = al.get(i);
                    if (os.getPosition() != null) {
                        OSAttribute osa = os.getAttribute(os.getDrawOnLayer());
                        if (osa != null) {
                            BufferedImage bi = (BufferedImage) osa.getAttrib().getImages().get(0);
                            Rectangle r = new Rectangle(os.getPosition().x, os.getPosition().y, bi.getWidth(), bi.getHeight());
                            if (r.contains(mapX, mapY)) {
                                comp.osControlPanel.setSpritePosition(e.getY(), e.getX(), false);
                            } else {
                                System.out.println("tidak");
                            }
                        }
                    }
                }
            } else if (comp.ToolbarSetBlock.isSelected()) {
                patternOperation(1);
            } else if (comp.ToolbarEraseBlock.isSelected()) {
                patternOperation(2);
            }
        } catch (Exception exc) {
        }
    }

    public void patternOperation(int mode) {
        int row = comp.lmControlPanel.getTable().getSelectedRow();
        Layer layer = (Layer) comp.layerContainer.getLayers().get(row);
        if (row != -1 && layer.isShow()) {
            if (mode == 1) {
                System.out.println("blokk");
                layer.getBlockTile()[tileX][tileY] = 1;
            } else if (mode == 2) {
                layer.getBlockTile()[tileX][tileY] = 0;
                System.out.println("no block");
            } else {
                Tile tile = comp.ipControlPanel.getCurrentTile();
                if (tile != null) {
                    if (mode == 3) {
                        layer.getTileContaner()[tileX][tileY] = tile;
                    } else {
                        layer.getTileContaner()[tileX][tileY] = null;
                    }
                }
            }
            comp.lmControlPanel.gridWorkspace.drawLayer(tileX, tileY);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
        drawTile(e);
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
        drawTile(e);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        setViewTile(e.getX(), e.getY());
    }

    public void setViewTile(int x, int y) {
        comp.InfoMapX.setText("x:" + x);
        comp.InfoMapY.setText("y:" + y);
        mapX = x;
        mapY = y;

        x = Math.max(pixelsToTiles(x, comp.project.getTileWidth()), 0);
        y = Math.max(pixelsToTiles(y, comp.project.getTileHeight()), 0);
        comp.InfoTileX.setText("x:" + x);
        comp.InfoTileY.setText("y:" + y);
        tileX = x;
        tileY = y;
    }

    public int getMapX() {
        return mapX;
    }

    public int getMapY() {
        return mapY;
    }

    public int getTileX() {
        return tileX;
    }

    public int getTileY() {
        return tileY;
    }

    public void stateChanged(ChangeEvent e) {
        if (comp.TabbedPane.getSelectedIndex() == 0) {
            comp.lmControlPanel.gridWorkspace.drawGrid();
            comp.ScrollPaneWorkspaceRender.repaint();
        }
        System.out.println(comp.TabbedPane.getSelectedIndex());
    }
}
